// package
package uit.comm.filters;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class HtmlResponseWrapper extends HttpServletResponseWrapper 
{
    protected static final String info    = "HtmlResponseWrapper/1.0";
    protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");


    public HtmlResponseWrapper(HttpServletRequest req, HttpServletResponse res, String encoding, int debug) throws IOException {
        super(res);
        String remote = req.getRemoteAddr();
        String requestURI = req.getRequestURI();
//        String servletPath = req.getServletPath();
        if (requestURI.endsWith(".html") || requestURI.endsWith(".html")) {
			res.setContentType("text/html; charset=" + encoding);
		} else if (requestURI.endsWith(".txt")) {
			res.setContentType("text/plain; charset=" + encoding);
		}

        if (debug > 0) {
            GregorianCalendar now = new GregorianCalendar();
            Date date = now.getTime();
            System.out.println(getInfo() + " was invoked at time " + sdf.format(date) + ",");
            System.out.println("    for " + requestURI + " which was requested from " + remote);
        }
    }

    public String getInfo() {
        return HtmlResponseWrapper.info;
    }
}
